package com.crawler.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

public class NumberUtil {

	/**
	 * 类型转换Double
	 * 
	 * @param doubleValue
	 * @return
	 */
	public static double getDouble(String doubleValue) {
		Logger log = Logger.getLogger(NumberUtil.class);
		String parseValue = doubleValue;
		try {
			doubleValue = doubleValue.replaceAll("-", "");
			doubleValue = doubleValue.replaceAll(" ", "");
			if (StringUtil.isNullOrEmpty(doubleValue))
				return 0;
			doubleValue = doubleValue.replaceAll(",", "");
			doubleValue = doubleValue.replaceAll("，", "");
			doubleValue = doubleValue.replaceAll("¥", "");
			doubleValue = doubleValue.replaceAll("¥", "");
			return Double.parseDouble(doubleValue);
		} catch (Exception e) {
			log.error("[Double转换错误转换的数值为:" + parseValue + "]");
			return 0;
		}
	}

	/**
	 * 获取double类型的排序数组,从高向低排序
	 * 
	 * @param objects
	 * @return
	 */
	public static double[] getDoubleSort(double... objects) {
		List<Double> sortDouble = new ArrayList<Double>();
		for (double object : objects) {
			if (object <= 0)
				continue;
			sortDouble.add(new Double(object));
		}
		if (sortDouble.size() > 0) {
			if (sortDouble.size() == 1) {
				double[] returnDouble = { sortDouble.get(0), sortDouble.get(0) };
				return returnDouble;
			}
			double[] sortDoubleCount = new double[sortDouble.size()];
			for (int ki = 0 ;ki<sortDouble.size();ki++) {
				sortDoubleCount[ki] = sortDouble.get(ki);
			}
			
			double temp; // 记录临时中间值
			int size = sortDouble.size(); // 数组大小
			for (int i = 0; i < size - 1; i++) {
				for (int j = i + 1; j < size; j++) {
					if (sortDoubleCount[i] < sortDoubleCount[j]) { // 交换两数的位置
						temp = sortDoubleCount[i];
						sortDoubleCount[i] = sortDoubleCount[j];
						sortDoubleCount[j] = temp;
					}
				}
			}
			return sortDoubleCount;
		}
		return null;
	}

	public static void main(String[] args) {
		double[] c = NumberUtil.getDoubleSort(new double[]{1});
		for (double d : c) {
			System.out.println("=="+d);
		}
	}
	
}
